package likou.offer;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @Info:
 * @Author: 唐小尊
 * @Date: 2020/8/5  22:02
 */
public class _27 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2);
        root.right = new TreeNode(7);
        root.left.left = new TreeNode(1);
        root.left.right = new TreeNode(3);
        root.right.left = new TreeNode(6);
        root.right.right = new TreeNode(9);


        tihuan(root);
        zhongxu(root);
    }

    public static TreeNode mirrorTree(TreeNode root) {
        tihuan(root);
        return root;
    }
    public static void tihuan(TreeNode node){
        if (node==null||(node.right==null&&node.left==null)){
            return;
        }
        TreeNode treeNode = node.left;
        node.left = node.right;
        node.right = treeNode;
        tihuan(node.left);
        tihuan(node.right);
    }

    public static void zhongxu(TreeNode root){
        Deque<TreeNode> deque = new LinkedList<>();
        TreeNode temp = root;
        while (deque.size()!=0||temp!=null){
            while (temp!=null){
                deque.push(temp);
                temp = temp.left;
            }
            if (deque.size() != 0){
                temp = deque.pop();
                System.out.println(temp.val);
                temp = temp.right;
            }
        }
    }
}
